//index.js
//获取应用实例
var app = getApp();
var utils = require('../../utils/utils');
Page({
  data: {
    isAllAct:false,
    getGoodLuck:false,//查询中奖记录
    noActivity:false,
    tabIndex:1,
    circleList: [],//圆点数组
    awardList: [],//奖品数组
    colorCircleFirst: '#FFDF2F',//圆点颜色1
    colorCircleSecond: '#FE4D32',//圆点颜色2
    colorAwardDefault: '#e4393c',//奖品默认颜色
    colorAwardSelect: '#52CB64',//奖品选中颜色
    indexSelect: 0,//被选中的奖品index
    isRunning: false,//是否正在抽奖
    activity:[]//动态奖品数组
  },

  onLoad: function () {
    var that = this;
    this.getActivity();

  },
  //设置界面
  setDom:function(){
    var that=this;
    //圆点设置
    var leftCircle = 7.5;
    var topCircle = 7.5;
    var circleList = [];
    for (var i = 0; i < 24; i++) {
      if (i == 0) {
        topCircle = 15;
        leftCircle = 15;
      } else if (i < 6) {
        topCircle = 7.5;
        leftCircle = leftCircle + 102.5;
      } else if (i == 6) {
        topCircle = 15
        leftCircle = 620;
      } else if (i < 12) {
        topCircle = topCircle + 94;
        leftCircle = 620;
      } else if (i == 12) {
        topCircle = 565;
        leftCircle = 620;
      } else if (i < 18) {
        topCircle = 570;
        leftCircle = leftCircle - 102.5;
      } else if (i == 18) {
        topCircle = 565;
        leftCircle = 15;
      } else if (i < 24) {
        topCircle = topCircle - 94;
        leftCircle = 7.5;
      } else {
        return
      }
      circleList.push({ topCircle: topCircle, leftCircle: leftCircle });
    }
    this.setData({
      circleList: circleList
    })
    //圆点闪烁
    setInterval(function () {
      if (that.data.colorCircleFirst == '#FFDF2F') {
        that.setData({
          colorCircleFirst: '#FE4D32',
          colorCircleSecond: '#FFDF2F',
        })
      } else {
        that.setData({
          colorCircleFirst: '#FFDF2F',
          colorCircleSecond: '#FE4D32',
        })
      }
    }, 500)
    //奖品item设置
    var awardList = [];
    //间距,怎么顺眼怎么设置吧.
    var topAward = 25;
    var leftAward = 25;
    for (var j = 0; j < 8; j++) {
      if (j == 0) {
        topAward = 25;
        leftAward = 25;
      } else if (j < 3) {
        topAward = topAward;
        //166.6666是宽.15是间距.下同
        leftAward = leftAward + 166.6666 + 15;
      } else if (j < 5) {
        leftAward = leftAward;
        //150是高,15是间距,下同
        topAward = topAward + 150 + 15;
      } else if (j < 7) {
        leftAward = leftAward - 166.6666 - 15;
        topAward = topAward;
      } else if (j < 8) {
        leftAward = leftAward;
        topAward = topAward - 150 - 15;
      }
      var activity = that.data.activity[j];
      //////console.log("奖品");
      //////console.log(that.data.activity);
      //////console.log("单个奖品");
      //////console.log(activity);
      awardList.push({ topAward: topAward, leftAward: leftAward, activity: activity });

    }
    //////console.log("图片数组");
    //////console.log(awardList);
    this.setData({
      awardList: awardList
    });
    //////console.log("awardList");
    //////console.log(awardList);
},
  //发送活动请求
  getActivity:function(){
    var that=this;
    ////console.log("发送活动请求");
    var httpObj={
      url:"shakehappy/shakeinfo",
      data:{
        who:app.globalData.who
      },
      success:function(res){
        if(res.data.status==1){
          //奖品详情
          //设置title
          utils.setNavigationBarTitle(res.data.shakedetails.shake.activeName);
          //设置界面奖品数组
          //是否显示奖品明细
          if(res.data.showPrize!=undefined){
            that.setData({
                  showPrize:res.data.showPrize
            });
          }else{
            that.setData({
              tabIndex:2
            });
          }
          console.log("活动说明");
          console.log(res.data);
          var shake=res.data.shakedetails.shake;
          that.setData({
            activeID:res.data.shakedetails.activeID,
            activity:res.data.shakedetails.Prize_details,
            shake:shake
          });
          that.setDom();
        }else{
          ////console.log(res.data.info);
          that.setData({
            noActivity:true
          });
          utils.modal('提示','暂时没有活动');
        }
      },
      fail:function(err){
        ////console.log(err);

      }
    };
    utils.http(httpObj);
  },
  //标签页的切换
  setTab:function(e){
    var that=this;
    //console.log("标签页的切换");
    //console.log(e.target.id);
    that.setData({
      tabIndex:e.target.id
    });
    //如果是中奖记录的话
    if(e.target.id==3){
      console.log('点击了中奖记录');
      if(that.data.luckList==undefined){
        console.log('可以加载中奖记录');
        that.getGoodLuck();
      }
    }
  },
  //发送参与活动的请求
  toActivity:function(){
    var that=this;
    //console.log("参与活动");
    var httpObj={
      url:"shakehappy/shaking",
      data:{
        who:app.globalData.who,
        act:that.data.activeID,
        openid:app.globalData.openid
      },
      method:"POST",
      success:function(res){
        if(res.data.status==1){
          //console.log(res.data);
          //console.log("抽到");
          that.setData({
            prize:res.data
          });
          //console.log("保存的奖品");
          //console.log("取到中奖商品的下标");
         that.getIndex(true);
          //刷新中奖记录
          that.getGoodLuck();
        }else{
          that.setData({
            prize:res.data
          });
         // //console.log("没抽到");
         // //console.log("保存谢谢参与下标");
         // //console.log(res.data.info);
         if(res.data.info=="今天次数用完了，明天再来。"){
           that.setData({
             isAllAct:true
           });
           return;
         }
          that.getIndex(false);
        }
      },
      fail:function(err){
        ////console.log(err);
      }
    };
    utils.http(httpObj);
  },
  //保存停留下标
  getIndex:function(whether){
    var that=this;
    ////console.log("保存停留下标");
    var awardList=that.data.awardList;
    if(whether==true){
      var prizeId=that.data.prize.prizeId;
    }
    if (whether == true) {
      //console.log("抽到奖品");
    for(var i=0;i<awardList.length;i++) {
        if (awardList[i].activity != undefined) {
          if (awardList[i].activity.id == prizeId) {
            that.setData({
              prizeIndex: i
            });
            ////console.log("找到下标" + that.data.prizeIndex)
          }
        }
      }
    }else{
      //console.log("没有抽到");
      for(var i=0;i<awardList.length;i++) {
        if(awardList[i].activity == undefined){
          that.setData({
            prizeIndex: i
          });
        }
      }
      //console.log("保存的下标"+i);
    }
  },
/////////////////////
  //开始游戏
  startGame: function () {
    var that=this;
    if(that.data.isAllAct){
      utils.modal('提示','今日次数已达上限');
      return;
    }
    that.toActivity();
    if (this.data.isRunning) return;
    this.setData({
      isRunning: true
    })
    var indexSelect = 0;
    var i = 0;
    var timer = setInterval(function () {
      indexSelect++;
      //这里我只是简单粗暴用y=30*x+200函数做的处理.可根据自己的需求改变转盘速度
      i += 30;
      indexSelect = indexSelect % 8;
      if (i > 1000) {
        //去除循环
        clearInterval(timer);
        if(that.data.prize.name!=undefined){
          utils.modal('恭喜你','获得了'+that.data.prize.name);
        }else{
          utils.modal('谢谢参与',that.data.prize.info);
        }
        that.setData({
          isRunning:false,
          prize:undefined
        });
        indexSelect=that.data.prizeIndex;
      }
      that.setData({
        indexSelect: indexSelect
      })
    }, (200 + i))
  },
  //查询中奖记录
  getGoodLuck:function(){
    var that=this;
    //console.log("查询中奖记录");
    var httpObj={
      url:"shakehappy/myshakelist",
      data:{
        who:app.globalData.who,
        act:that.data.activeID,
        openid:app.globalData.openid
        //userid:app.globalData.userid
      },
      method:'POST',
      success:function(res){
        //console.log(res);
        if(res.data.status==1){
          that.setData({
            luckList:res.data.prizelist,
           getGoodLuck:true
          });
        }else{
          utils.modal('提示',res.data.info);
          //console.log(res.data.info);
        }
      }
    };
    //console.log(app.globalData);
    //console.log(httpObj);
    utils.http(httpObj);
  },
  //分享
  onShareAppMessage: function () {
    return {
      title: app.globalData.appData.sharetitle || "发现一家有趣的活动，和你分享一下",
      path: "/pages/activity/index"
    }
  },
  //监听下拉
  onPullDownRefresh:function(){
    //console.log("下拉刷新");
    wx.stopPullDownRefresh();
  },
});
